package com.deschen.security.core.validate.code.controller;

import com.deschen.security.core.properties.SecurityConstants;
import com.deschen.security.core.validate.code.ValidateCode;
import com.deschen.security.core.validate.code.ValidateCodeProcessor;
import com.deschen.security.core.validate.code.ValidateCodeProcessorHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author deschen
 * @Create 2019/8/17
 * @Description 校验控制层
 * @Since 1.0.0
 */
@RestController
@Slf4j
public class ValidateCodeController {

    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

//    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";

//    @Autowired
//    @Qualifier("imageCodeGenerator")
//    private ValidateCodeGenerator ImageCodeGenerator;
//
//    @Autowired
//    @Qualifier("smsCodeGenerator")
//    private ValidateCodeGenerator SmsCodeGenerator;
//
//    @Autowired
//    private SmsValidateCodeSender smsCodeSender ;

    @Autowired
    private ValidateCodeProcessorHolder validateCodeProcessorHolder;


    /**
     * 获取验证码图片
     *
     * @param request
     * @param response
     */
    @GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{type}")
    public void getValidateCode(HttpServletRequest request, HttpServletResponse response, @PathVariable String type) throws Exception {
//        // 创建验证码图片
//        ImageValidateCode imageValidateCode = (ImageValidateCode) ImageCodeGenerator.createImageCode(request);
//        // 存在session中
//        sessionStrategy.setAttribute(new ServletWebRequest(request), SESSION_KEY, imageValidateCode);
//        // response输出流图片
//        ImageIO.write(imageValidateCode.getImage(), "JPEG", response.getOutputStream());
        log.info("【" + type + "验证码】控制成接受 account = {}", request.getParameter("account"));
        validateCodeProcessorHolder.findValidateCodeProcessorByType(type)
                .process(new ServletWebRequest(request, response), request.getParameter("account"));
        log.info("【" + type + "图片验证码】控制成发送成功");
    }

    @GetMapping("/code/ValidateCode/{type}")  // todo 测试用
    public void getValidateCodeInSession(HttpServletRequest request, HttpServletResponse response, @PathVariable String type) {

        ValidateCode validateCode = (ValidateCode) sessionStrategy.getAttribute(
                new ServletWebRequest(request, response),
                ValidateCodeProcessor.SESSION_KEY_PREFIX + type + "_" + request.getParameter("account")
        );
        log.info("【图片验证码】控制层获取保存在session的ImageValidateCode = {}", validateCode);
    }


//    /**
//     *
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    @GetMapping("/code/mobile")
//    public void getCodeSms(HttpServletRequest request, HttpServletResponse response){
//        // 创建验证码手机验证码
//        ValidateCode validateCode = SmsCodeGenerator.createImageCode(request);
//        // 存在session中
//        sessionStrategy.setAttribute(new ServletWebRequest(request), SESSION_KEY, validateCode);
//        // 发送手机验证码
//        smsCodeSender.send(request.getParameter("mobile"), validateCode.getCode());
//
//    }
}
